package cn.edu.zafu.bigdata.x.zcy_0217;

public class ArrayIndexOutOfBoundsExceptionDemo {

    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        int index = 10; // 这个索引超出了数组的范围

        try {
            int element = getElement(array, index);
            System.out.println("元素是: " + element);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("错误：索引超出数组范围！");
        }
    }

    public static int getElement(int[] arr, int index) {
        if (index < 0 || index >= arr.length) {
            throw new ArrayIndexOutOfBoundsException("索引超出数组范围");
        }
        return arr[index];
    }
}
